define(["utils","base"],function(utils,baseClass){

    var Component = function(config){
        var _this = this;

        Component.baseConstructor.call(this,config);
        this.$el.addClass("yy-view");

        var pluginLoadMoreMethodName = this.config.comKey+"_loadmore";
        this.pluginLoadMoreMethod = this.pageview.plugin[pluginLoadMoreMethodName];
        this.initLayout(this.datasource,null,this.rowInstance,function(){
          _this.initEvent();
        });
        this.LoadMoreOffsetBottom = 10;
        this.canLoadMore = true;


        var pluginLoadMoreErrorToReload = this.config.comKey+"_reload";
        this.pluginLoadMoreErrorToReloadMethod = this.pageview.plugin[pluginLoadMoreErrorToReload];

    }

    utils.extendClass(Component,baseClass);

    Component.prototype._initLoadMoreDom = function(){
    	this.loadMoreDom = $("<div class='yy-view-loadmore-dom displaynone'></div>");
    	this.loadMoreBox = $("<div class='yy-view-loadmore-box'></div>");
    	this.loadMoreText = $("<div class='yy-view-loadmore-text'></div>");
    	this.loadMoreDom.append(this.loadMoreBox).append(this.loadMoreText);

    	this.$el.append(this.loadMoreDom);
    };
    Component.prototype._initLoadMoreErrorDom = function(){
      var _this = this;
      this.loadMoreErrorDom = $("<div class='yy-view-loadmore-err-wrap displaynone'></div>");
      var reloadLabel = $("<span class='yy-view-loadmore-err-label'>网络异常,<span>点击加载</span></span>");
      reloadLabel.bind("click",function(){
        _this.pluginLoadMoreErrorToReloadMethod &&_this.pluginLoadMoreErrorToReloadMethod.call(_this.pageview.plugin,_this,{});
      });
      this.loadMoreErrorDom.append(reloadLabel);

    	this.$el.append(this.loadMoreErrorDom);
    };

    Component.prototype.setLoadMoreErrorState = function(){
      this.canLoadMore = false;
      this.loadMoreDom&&this.loadMoreDom.addClass("displaynone");
      this.loadMoreErrorDom&&  this.loadMoreErrorDom.removeClass("displaynone");
    };

    Component.prototype.setCanLoadMore = function(){
    	this.canLoadMore = true;
      this.loadMoreErrorDom&&  this.loadMoreErrorDom.addClass("displaynone");
    	this.loadMoreBox&&this.loadMoreBox.spin('tiny', '#999');
    	this.loadMoreText&&this.loadMoreText.html("正在加载...");
    	this.loadMoreDom&&this.loadMoreDom.removeClass("displaynone");
    };
    Component.prototype.setNoData = function(){
    	this.loadMoreText&&this.loadMoreText.html("");
    };
    Component.prototype.hideLoadMore = function(){
    	this.loadMoreDom&&this.loadMoreDom.addClass("displaynone");
    };
    Component.prototype.showLoadMore = function(){
        //fixed here
      // if(!this.canLoadMore){
      //   this.setHasLoadDone();
      //   return;
      // }
    	this.setCanLoadMore();
    };
    Component.prototype.setHasLoadDone = function(){
    	this.canLoadMore = false;
        if(this.loadMoreBox){
            this.loadMoreBox.spin(false);
            this.loadMoreText.html("已加载全部数据");
            this.loadMoreDom.removeClass("displaynone");
        }
    };
    Component.prototype._onScroll = function(){

      if(!this.canLoadMore){
        return;
      }
      if(this.$el[0].offsetHeight+this.$el.scrollTop()+this.LoadMoreOffsetBottom>=this.$el[0].scrollHeight){
        this.pluginLoadMoreMethod.call(this.pageview.plugin,this,{});
      }
    };
    Component.prototype.initEvent  =function(){
    	var _this = this;

    	if(this.pluginLoadMoreMethod){
        this._initLoadMoreDom();

        this._initLoadMoreErrorDom();
        var scrollTimeId = null;
        this.$el.bind("scroll",function(e){
          if(scrollTimeId){window.clearTimeout(scrollTimeId)}
          scrollTimeId = window.setTimeout(function(){
            _this._onScroll(e);
          },80);
        });
    	}
    }


    return Component;
});
